package com.blb.day12_spring_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.day12_spring_boot.entity.User;
import com.blb.day12_spring_boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public String login(Model model, HttpSession session, String username, String password){
        User user = userService.getOne(new QueryWrapper<User>().eq("username", username)
                .eq("password", password));
        if(user == null){
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }
        session.setAttribute("user",user);
        return "redirect:/book/list";
    }
}
